﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;

namespace 语音识别
{
    public class HttpClientHelper
    {
        private static readonly object LockObj = new object();
        private static HttpClient client = null;
        public HttpClientHelper()
        {
            GetInstance();
        }
        public static HttpClient GetInstance()
        {

            if (client == null)
            {
                lock (LockObj)
                {
                    if (client == null)
                    {
                        client = new HttpClient();
                    }
                }
            }
            return client;
        }
        public async Task<string> PostAsync(string url, string strJson)//post异步请求方法
        {
            try
            {
                HttpContent content = new StringContent(strJson);
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                //由HttpClient发出异步Post请求
                HttpResponseMessage res = await client.PostAsync(url, content);
                if (res.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    string str = res.Content.ReadAsStringAsync().Result;
                    return str;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public string Post(string url, string strJson)//post同步请求方法
        {
            try
            {
                HttpContent content = new StringContent(strJson);
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                //client.DefaultRequestHeaders.Connection.Add("keep-alive");
                //由HttpClient发出Post请求
                Task<HttpResponseMessage> res = client.PostAsync(url, content);
                if (res.Result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    string str = res.Result.Content.ReadAsStringAsync().Result;
                    return str;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public void PostFile(string url, string filePath, string paraName, string fileName, SpeechSynthesizer ssh, Action<string, SpeechSynthesizer> action)
        {
            MultipartFormDataContent content = new MultipartFormDataContent();
            
                content.Add(new ByteArrayContent(File.ReadAllBytes(filePath)), paraName, fileName);
                Task.Run(async () => testc(client,url,content,ssh,action)).Wait();
            
        }

        public async void testc(HttpClient client,string url, MultipartFormDataContent content, SpeechSynthesizer ssh, Action<string, SpeechSynthesizer> action) {
            var response = await client.PostAsync(url, content);
            var data = response.Content.ReadAsStringAsync().Result;
            VoiceRes item = new VoiceRes();
            // Console.WriteLine(item);
            // Console.WriteLine(ssh);
            try {
                item = JsonConvert.DeserializeObject<VoiceRes>(data);
                ssh.SpeakAsync(item.data.text);
            }
            catch (Exception ex) {
                Console.WriteLine(item);
            }
            action(data, ssh);
        }

        public string Get(string url)
        {
            try
            {
                var responseString = client.GetStringAsync(url);
                return responseString.Result;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

    }
}
